package Demo2;

class student {
    String id;//学号
    private String name;//私有属性 姓名
    private int age;//年龄
    private int gender;//性别 0男 1女
    public void print() {
        System.out.println("id:" + id + ";name;" + name + ";age;" + age);
    }
    /**
     * 当需要使用私有属性时通过封装方法访问
     */
    public String getName(){
        return name;
    }
    public void setName(String s){
        name = s;
    }
    /**
     * 封装设置年龄
     */
    public  void  setAge(int n){
        if (n <= 0);{
            System.out.println("年龄非法");
        }
        if (n >= 200){
            System.out.println("年龄超出200岁,非法!");
        }
        age = n;
    }
    /**
     * 封装设置获取性别
     */
    public void setGender(String s){
        if (s.equals('男')){
            gender = 0;
        }else {
            gender = 1;
        }
    }
    public String getGender(){
        String g;
        if (gender == 0){
            g = "男";
        }else {
            g = "女";
        }
        return  g;
    }
}

public class Demo02 {
    public static void main(String[] args) {
        student stu1 = new student();
        stu1.setName("猪小明");
        System.out.println(stu1.getName());
        System.out.println(stu1.getName());
    }
}